#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

#variable for all chromium versions supported
VERSIONS="65
72
74
78
84
86
88"

#chromium version can be specified by $1
#usage: /path/to/this-script 78
if [ -z "$1" ];then
  LIST="$(echo "$VERSIONS" | sed 's/^/false\nChromium version /g')"
  
  output=''
  while [ -z "$output" ];do
    output="$(echo "$LIST" | yad --center --title="Choose Chromium version" --height=290 --no-headers \
      --text="  Which Chromium version to install today?  " \
      --radiolist --list --column=Tick --column=version --separator='\n' --print-column=2 \
      --window-icon="$(dirname "$0")/icon-64.png" \
      --button=!${DIRECTORY}/icons/check.png!OK:0
    )"
    button=$?
    
    [ $button != 0 ] && error "exited chromium version selection list"
  done
  #get just chromium version NUMBER
  output="$(echo "$output" | awk '{print $3}')"
else
  output="$1"
fi
#wait until chromium is not running
if [ ! -z "$(ps aux | grep chromium | grep -v 'grep' )" ];then
  echo -n "Please close all Chromium windows first."
  yad --info --center --window-icon="$(dirname "$0")/icon-64.png" \
  --title="Close Chromium first" --text="$(echo -e "  Chromium is running.\n  To reduce the risk of Chromium profile corruption, please close Chromium first.  ")" \
  --window-icon="$(dirname "$0")/icon-64.png" --no-buttons &
  yadpid=$!
  while [ ! -z "$(ps aux | grep chromium | grep -v 'grep' )" ];do
    echo -n '.'
    sleep 1
  done
  echo
  kill $yadpid
fi

#All urls acquired from http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/

#Set url variables based on selected version number
case "$output" in

  65)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_65.0.3325.181-0+rpt4_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_65.0.3325.181-0+rpt4_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_65.0.3325.181-0+rpt4_armhf.deb"
    ;;
    
  72)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_72.0.3626.121-0+rpt4_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_72.0.3626.121-0+rpt4_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_72.0.3626.121-0+rpt4_armhf.deb"
    ;;
    
  74)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_74.0.3729.157-rpt5_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_74.0.3729.157-rpt5_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_74.0.3729.157-rpt5_armhf.deb"
    ;;
    
  78)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_78.0.3904.108-rpt1_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_78.0.3904.108-rpt1_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_78.0.3904.108-rpt1_armhf.deb"
    ;;
    
  84)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_84.0.4147.105-rpt3_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_84.0.4147.105-rpt3_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_84.0.4147.105-rpt3_armhf.deb"
    ;;
    
  86)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_86.0.4240.197-rpt1_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_86.0.4240.197-rpt1_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_86.0.4240.197-rpt1_armhf.deb"
    ;;
  
  88)
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_88.0.4324.187-rpt1_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_88.0.4324.187-rpt1_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_88.0.4324.187-rpt1_armhf.deb"
    ;;
  92)
    #this is the first RPF release of chromium arm64 debs
    cb_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser_92.0.4515.98~buster-rpt1_armhf.deb"
    l10n_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-browser-l10n_92.0.4515.98~buster-rpt1_all.deb"
    ffmpeg_extra_url="http://archive.raspberrypi.org/debian/pool/main/c/chromium-browser/chromium-codecs-ffmpeg-extra_92.0.4515.98~buster-rpt1_armhf.deb"
    ;;
  *)
    error "unrecognized chromium version '$output'."
    ;;
esac

#back up chromium config directory
if [ ! -d ~/.config/chromium.bak ];then
  echo "Backing up existing Chromium profile directory..."
  rm -rf ~/.config/chromium.bak
  
  cp -a ~/.config/chromium ~/.config/chromium.bak
  
fi

#download the files
echo "Downloading the packages..."
rm -f cb.deb l10n.deb ffmpeg_extra.deb || error "Failed to remove cb.deb l10n.deb ffmpeg_extra.deb"
wget -O cb.deb "$cb_url" || error "Failed to download $cb_url"
wget -O l10n.deb "$l10n_url" || error "Failed to download $l10n_url"
wget -O ffmpeg_extra.deb "$ffmpeg_extra_url" || error "Failed to download $ffmpeg_extra_url"

echo "Installing the packages..."
"${DIRECTORY}/api" apt_lock_wait
sudo apt install -fy --allow-downgrades --allow-change-held-packages "$(pwd)/cb.deb" "$(pwd)/l10n.deb" "$(pwd)/ffmpeg_extra.deb" || error "APT failed to install the packages"
rm -f cb.deb l10n.deb ffmpeg_extra.deb || error "Failed to remove cb.deb l10n.deb ffmpeg_extra.deb"

echo "Holding Chromium version so it won't upgrade..."
"${DIRECTORY}/api" apt_lock_wait
echo "chromium-browser hold" | sudo dpkg --set-selections
"${DIRECTORY}/api" apt_lock_wait
echo "chromium-browser-l10n hold" | sudo dpkg --set-selections
"${DIRECTORY}/api" apt_lock_wait
echo "chromium-codecs-ffmpeg-extra hold" | sudo dpkg --set-selections

#echo "Fixing Chromium profile..."
#rm ~/'.config/chromium/Default/Web Data' &>/dev/null

